
package com.ucpaas.sms.action.componentConf;

import java.util.*;

import com.jsmsframework.common.util.JsonUtil;
import com.jsmsframework.middleware.exception.JsmsComponentConfigureException;
import com.jsmsframework.middleware.service.JsmsComponentConfigureService;
import com.ucpaas.sms.constant.LogConstant;
import com.ucpaas.sms.constant.SysConstant;
import com.ucpaas.sms.enums.LogEnum;
import com.ucpaas.sms.service.LogService;
import com.ucpaas.sms.util.web.AuthorityUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ucpaas.sms.action.BaseAction;
import com.ucpaas.sms.service.componentConf.SmspComponentService;
import com.ucpaas.sms.util.web.StrutsUtils;

/**
 * 组件管理-SMSP组件配置
 */
@Controller
@Scope("prototype")
@Results({@Result(name = "query", location="/WEB-INF/content/componentConf/smspComponent/query.jsp"),
		@Result(name = "edit", location="/WEB-INF/content/componentConf/smspComponent/edit.jsp")})

public class SmspComponentAction extends BaseAction {
	
	private static final long serialVersionUID = 3959353274127958317L;
	@Autowired
	private SmspComponentService smspComponentService;
	@Autowired
	private JsmsComponentConfigureService jsmsComponentConfigureService;
	@Autowired
	private LogService logService;

	
	@Action("/componentConf/smspComponent/query")
	public String query() {
		Map<String, String> params = StrutsUtils.getFormData();
		page = smspComponentService.query(params);
		StrutsUtils.setAttribute("component_type", params.get("component_type"));
		return "query";
	}
	
	@Action("/componentConf/smspComponent/edit")
	public String add() {
		String id = StrutsUtils.getParameterTrim("id");
		if (NumberUtils.isDigits(id)) {
			data = smspComponentService.editView(Integer.parseInt(id));
		} else {
			data = new HashMap<String, Object>();
		}
		return "edit";
	}
	
	@Action("/componentConf/smspComponent/save")
	public void save() {
		Map<String, String> params = StrutsUtils.getFormData();
		data = smspComponentService.save(params);
		StrutsUtils.renderJson(data);
	}
	
	@Action("/componentConf/smspComponent/delete")
	public void delete() {
		String componentId = StrutsUtils.getParameterTrim("component_id");
		Map<String, String> params = new HashMap<String, String>();
		params.put("component_id", componentId);
		data = smspComponentService.delete(params);
		StrutsUtils.renderJson(data);
	}


	@Action("/componentConf/smspComponent/ComponentSwitch")
	public void updateComponentSwitch(){
		Map<String, String> params = StrutsUtils.getFormData();
		Map<String, Object> result = new HashMap<String, Object>();
		String idstr=params.get("ids");
		String comswitch=params.get("switch");

		if(StringUtils.isBlank(idstr)||!NumberUtils.isDigits(comswitch)){
			result.put("result", com.jsmsframework.common.constant.SysConstant.FAIL);
			result.put("msg","参数非法");
			StrutsUtils.renderJson(result);
			return;
		}
		Set<Integer> ids = new HashSet<>();
		String[] idsArray=idstr.split(",");
		for (String s : idsArray) {
			Integer sid=Integer.valueOf(s).intValue();
			ids.add(sid);
		}
		int up=jsmsComponentConfigureService.updateSwitch(ids,Integer.valueOf(comswitch).intValue());
		if(up>0){
			logService.add(LogConstant.LogType.update, LogEnum.组件配置.getValue(),"组件配置-SMSP组件配置：SMSP组件配置消费者开关成功k","操作数据："+ JsonUtil.toJson(params),"当前成功:"+JsonUtil.toJson(params),"操作人："+ AuthorityUtils.getLoginRealName());
			result.put("result", com.jsmsframework.common.constant.SysConstant.SUCCESS);
			result.put("msg","操作成功");
		}else {
			result.put("result", com.jsmsframework.common.constant.SysConstant.FAIL);
			result.put("msg","操作失败");
			StrutsUtils.renderJson(result);
			logService.add(LogConstant.LogType.update, LogEnum.组件配置.getValue(),"组件配置-SMSP组件配置：SMSP组件配置消费者开关失败","操作数据："+JsonUtil.toJson(params),"当前失败:"+JsonUtil.toJson(params),"操作人："+ AuthorityUtils.getLoginRealName());
			throw new JsmsComponentConfigureException("更新消费者开发失败");
		}
		StrutsUtils.renderJson(result);

	}
}
